
// API的基地址
const BASE_URL = 'http://localhost:3001/api';

/**
 * 封装的请求函数
 * @param {string} url - 请求路径
 * @param {string} method - 请求方法
 * @param {object} data - 请求体
 */
const request = (url, method, data) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${BASE_URL}${url}`,
      method,
      data,
      success: (res) => {
        if (res.statusCode >= 200 && res.statusCode < 300) {
          resolve(res.data);
        } else {
          console.error('Request failed with status:', res.statusCode, res.data);
          reject(res);
        }
      },
      fail: (err) => {
        console.error('Request failed:', err);
        reject(err);
      },
    });
  });
};

/**
 * @description 获取所有商品和分类
 * @returns {Promise<object>}
 */
export const fetchProducts = () => {
  return request('/products', 'GET');
};

/**
 * @description 提交订单到房账
 * @param {object} orderData - { roomId, items, totalAmount }
 * @returns {Promise<object>}
 */
export const submitRoomOrder = (orderData) => {
  return request('/orders', 'POST', orderData);
};

/**
 * @description 获取指定包厢的订单列表
 * @param {string} roomId
 * @returns {Promise<Array>}
 */
export const fetchRoomOrders = (roomId) => {
  return request(`/orders/room/${roomId}`, 'GET');
};
